package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.atguigu.jxc.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public Map<String,Object> getGoodsList(Integer page,Integer rows, String goodsCodeOrName, String goodsTypeId) {
        //判断  穿过类的是 goodsCode 还是 goodeName
        String goodsCode = null;
        String goodsName = null;

        //分页数据处理
        page = (page - 1) * rows;

        //商品列表
        List<Goods> goodsList = goodsDao.getGoodsList(page, rows, goodsCode, goodsName, goodsTypeId);

        //每一个商品赋值  商品分类名称 商品库存数量
        for (Goods goods : goodsList) {
            //商品名称
            goods.setGoodsTypeName(goodsDao.getGoodsTypeName(goods.getGoodsTypeId().toString()));
            //销售总量
            goods.setSaleTotal(goodsDao.getSaleTotal(goods.getGoodsId()));
        }

        //商品总条数
        Integer goodsTotal = getGoodsTotal();

        //封装返回数据
        Map<String,Object> map = new HashMap<>();
        map.put("rows",goodsList);
        map.put("total",goodsTotal);

        return map;
    }


    @Override
    public Integer getGoodsTotal() {
        return goodsDao.getGoodsTotal();
    }

    /**
     * 商品管理里面的分页获取商品列表
     * @param page
     * @param rows
     * @param goodsName
     * @param goodsTypeId
     * @return
     */
    @Override
    public Map<String, Object> getGoodsPge(Integer page, Integer rows, String goodsName, String goodsTypeId) {

        //分页数据处理
        page = (page - 1) * rows;

        //获取商品数据
        List<Goods> goodsList = goodsDao.getGoodsList(page, rows, null, goodsName, goodsTypeId);

        //完善数据
        for (Goods goods : goodsList) {
            //商品名称
            goods.setGoodsTypeName(goodsDao.getGoodsTypeName(goods.getGoodsTypeId().toString()));
            //销售总量
            goods.setSaleTotal(null);
        }

        //商品总条数
        Integer goodsTotal = getGoodsTotal();

        //封装返回数据
        Map<String,Object> map = new HashMap<>();
        map.put("rows",goodsList);
        map.put("total",goodsTotal);

        return map;
    }

    @Override
    public int save(Goods goods) {
        return goodsDao.save(goods);
    }

    @Override
    public int updateById(Goods goods) {
        return goodsDao.updateById(goods);
    }

    @Override
    public Goods getInfoById(String goodsId) {
        return goodsDao.getInfoById(goodsId);
    }

    @Override
    public int deleteById(String goodsId) {
        return goodsDao.deleteById(goodsId);
    }

    @Override
    public List<Goods> getInvenPage(Integer page, Integer rows, String nameOrCode) {
        //判断传过来的参数
        String goodsName = null;
        String goodsCode = null;

        //如果是纯数字就是编号
        if(!StringUtil.isEmpty(nameOrCode)){
            if(nameOrCode.matches("[0-9]+"))
                goodsCode = nameOrCode;
            else
                goodsName = nameOrCode;
        }

        //解决分页数据
        page = (page - 1) * rows;

        List<Goods> invenPage = goodsDao.getInvenPage(page, rows, goodsName, goodsCode);

        return invenPage;
    }

    @Override
    public int getInvenTotal() {
        return goodsDao.getInvenTotal();
    }

    @Override
    public List<Goods> getHashPage(Integer page, Integer rows, String nameOrCode) {
        //判断传过来的参数
        String goodsName = null;
        String goodsCode = null;

        //如果是纯数字就是编号
        if(!StringUtil.isEmpty(nameOrCode)){
            if(nameOrCode.matches("[0-9]+"))
                goodsCode = nameOrCode;
            else
                goodsName = nameOrCode;
        }

        //解决分页数据
        page = (page - 1) * rows;

        List<Goods> invenPage = goodsDao.getInvenPage(page, rows, goodsName, goodsCode);

        return invenPage;
    }

    @Override
    public int getHashTotal() {
        return goodsDao.getHashTotal();
    }

    @Override
    public int updateInfo(String goodsId, Integer inventoryQuantity, Double purchasingPrice) {
        return goodsDao.updateInfo(goodsId,inventoryQuantity,purchasingPrice);
    }

    @Override
    public int updateInven(String goodsId) {
        return goodsDao.updateInven(goodsId);
    }


    @Override
    public List<Goods> getListAlarm() {

        //获取商品数据
        List<Goods> goodsList = goodsDao.getListAlarm();

        //完善数据
        for (Goods goods : goodsList) {
            //商品名称
            goods.setGoodsTypeName(goodsDao.getGoodsTypeName(goods.getGoodsTypeId().toString()));
            //销售总量
            goods.setSaleTotal(null);
        }



        //封装返回数据
        return goodsList;
    }


}
